package com.central.operate.controller;

import com.central.model.common.PageResult;
import com.central.operate.model.LoginInfo;
import com.central.operate.model.OperateRecord;
import com.central.operate.model.RecordTime;
import com.central.operate.service.LoginCountService;
import com.central.operate.vo.LoginCountVO;
import com.central.operate.vo.RegisterCountChartVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 登录量控制类
 */
@Slf4j
@RestController
public class LoginCountConterller {

    @Autowired
    private LoginCountService loginCountService;

    /*@PreAuthorize("hasAuthority('operate:get/resource')")
    @ApiOperation(value = "玩家登录量")
    @GetMapping("/loginCount/{time}")
    public PageResult<RecordTime> getResource(@PathVariable String time) {
        Long s = new Date().getTime();
        log.info("传入时间" + time);
        Map<String, Object> params = new HashMap<>();
        if (time != null){
            params.put("startTime",time + " 00:00:00");
            params.put("endTime",time + " 23:59:59");
        }
        PageResult<RecordTime> record =loginCountService.listLoginCount(params);
        System.out.println("耗时:" + (new Date().getTime() - s));
        return record;
    }*/
    @PreAuthorize("hasAuthority('operate:get/resource')")
    @ApiOperation(value = "玩家登录量")
    @GetMapping("/loginCount/{time}")
    public PageResult<LoginCountVO> getResource(@PathVariable String time) {
        Long s = new Date().getTime();
        log.info("传入时间" + time);
        PageResult<LoginCountVO> record =loginCountService.listLoginCount(time);
        System.out.println("耗时:" + (new Date().getTime() - s));
        return record;
    }

    @PreAuthorize("hasAuthority('operate:get/online')")
    @ApiOperation(value = "实时在线玩家")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"), @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")})
    @GetMapping("/online")
    public PageResult<LoginInfo> online(@RequestParam Map<String, Object> params) {
        params.forEach((k, v) -> log.info(k + ":" + v));
        return loginCountService.listOnline(params);
    }


    @PreAuthorize("hasAuthority('operate:get/register')")
    @ApiOperation(value = "玩家登录量:图表")
    @GetMapping("/loginCount")
    public List<RegisterCountChartVO> getloginChart(@RequestParam Map<String, Object> params) {
        Long s = new Date().getTime();
        log.info("传入时间" + params);
        params.forEach((k, v) -> log.info(k + " : " + v));
        List<RegisterCountChartVO> record = loginCountService.listLoginCount(params);
        System.out.println("耗时:" + (new Date().getTime() - s));
        return record;
    }

    @PreAuthorize("hasAuthority('operate:get/register')")
    @ApiOperation(value = "玩家在线量:图表")
    @GetMapping("/onlineCount")
    public List<RegisterCountChartVO> getOnlineChart(@RequestParam Map<String, Object> params) {
        Long s = new Date().getTime();
        log.info("传入时间" + params);
        params.forEach((k, v) -> log.info(k + " : " + v));
        List<RegisterCountChartVO> record = loginCountService.listOnlineCount(params);
        System.out.println("耗时:" + (new Date().getTime() - s));
        return record;
    }

}
